package cn.sdormitory.controller.wechat;

import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.common.exception.ApiException;
import cn.sdormitory.wx.service.WechatOpenIdService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/wechat/setOpen")
public class WechatOpenIdController {
    @Autowired
    private WechatOpenIdService wechatOpenIdService;

    @ApiOperation("学生,家长身份绑定，发送手机验证码")
    @GetMapping(value = "/sendValidCode")
    public CommonResult<String> sendValidCode(@RequestParam("phone") String phone) throws ApiException{
        wechatOpenIdService.sendValidCode(phone);
        return CommonResult.success("发送成功");
    }

    @ApiOperation("学生身份绑定，储存openId")
    @GetMapping(value = "/updateStuWxOpenId")
    public CommonResult updateStuWxOpenId(@RequestParam Map<String, Object> params){
        String name=(String) params.get("name");
        String phone=(String) params.get("phone");
        String code=(String) params.get("code");
        String openId=(String) params.get("openId");
        wechatOpenIdService.updateStuWxOpenId(name,phone,code,openId);
        return CommonResult.success();
    }
    @ApiOperation("家长身份绑定，储存openId")
    @GetMapping(value = "/updateParentWxOpenId")
    public CommonResult updateParentWxOpenId(@RequestParam Map<String, Object> params){
        String studentName=(String) params.get("studentName");
        String studentPhone=(String) params.get("studentPhone");
        String phone=(String) params.get("phone");
        String code=(String) params.get("code");
        String openId=(String) params.get("openId");
        wechatOpenIdService.updateParentWxOpenId(studentName,studentPhone,phone,code,openId);
        return CommonResult.success();
    }
    @ApiOperation("班主任身份绑定，储存openId")
    @GetMapping(value = "/updateTeacherWxOpenId")
    public CommonResult updateTeacherWxOpenId(@RequestParam Map<String, Object> params){
        String accessNo=(String) params.get("accessNo");
        String password=(String) params.get("password");
        String openId=(String) params.get("openId");
        wechatOpenIdService.updateTeacherWxOpenId(accessNo,password,openId);
        return CommonResult.success();
    }
}
